package com.leather.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leather.admin.entity.Admin;
import com.leather.admin.entity.Permission;
import com.leather.admin.mapper.AdminMapper;
import com.leather.admin.service.AdminService;
import com.leather.admin.service.RoleService;
import com.leather.admin.vo.AdminRoleVo;
import com.leather.admin.vo.AdminVo;
import com.leather.common.result.CodeMsg;
import com.leather.common.result.Result;
import com.leather.common.toolkits.EncryptKit;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {


    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private RoleService roleService;

    @Override
    public Admin getAdminById(String username) {
        if (StringUtils.isBlank(username)) {
            return null;
        }
        return query().eq("admin_name", username).one();
    }

    @Override
    public Result addAdmin(AdminVo adminVo) {
        Result result = adminVo.validateAdd();
        if (!result.isSuccess()) {
            return result;
        }
        if (null == roleService.getRoleById(adminVo.getRoleId())) {
            return Result.error(CodeMsg.ROLE_NOT_EXIST);
        }
        if (null != getAdminById(adminVo.getAdminName())) {
            return Result.error(CodeMsg.DUPPY_ADMINNAME);
        }
        Admin admin = (Admin) result.getData();
        admin.setPassword(EncryptKit.MD5(admin.getPassword()));
        if (save(admin)) {
            return Result.success(admin.getAdminName());
        }
        return Result.error(CodeMsg.SERVER_ERROR);

    }

    @Override
    public Result changePwd(AdminVo adminVo) {
        Result result = adminVo.validateChange();
        if (!result.isSuccess()) {
            return result;
        }
        Admin ad = getAdminById(adminVo.getAdminName());
        if (null == ad) {
            return Result.error(CodeMsg.ERROR_ADMIN_NAME);
        }
        ad.setPassword(EncryptKit.MD5(adminVo.getPassword()));
        if (updateById(ad)) {
            return Result.success(ad.getAdminName());
        }
        return Result.error(CodeMsg.SERVER_ERROR);
    }

    @Override
    public Result changeAdminRole(int adminId, int roleId) {
        if (adminId <= 0) {
            return Result.error(CodeMsg.ERROR_ADMIN);
        }
        if (roleId <= 0) {
            return Result.error(CodeMsg.ERROR_ROLE);
        }
        Admin admin = getById(adminId);
        if (null == admin) {
            return Result.error(CodeMsg.ERROR_ADMIN);
        }
        if (null == roleService.getById(roleId)) {
            return Result.error(CodeMsg.ERROR_ROLE);
        }
        admin.setRoleId(roleId);
        if (updateById(admin)) {
            return Result.success(2);
        }
        return Result.error(CodeMsg.SERVER_ERROR);
    }

    @Override
    public Result frozenAdmin(int adminId) {

        int result = adminMapper.fronzenAdmin(adminId);
        if (result <= 0) {
            return Result.error(CodeMsg.ERROR_ADMIN);
        }
        return Result.success(adminId);
    }

    @Override
    public Result unFrozenAdmin(int adminId) {
        int result = adminMapper.unFronzenAdmin(adminId);
        if (result <= 0) {
            return Result.error(CodeMsg.ERROR_ADMIN);
        }
        return Result.success(adminId);
    }


    @Override
    public List<String> getAdminPermission(String adminName) {
        List<String> persmissons = new ArrayList<>();
        List<Permission> adminPermissions = adminMapper.getAdminPermissions(adminName);
        adminPermissions.forEach((v) -> {
            if (StringUtils.isNotBlank(v.getPermissionValue())) {
                persmissons.add(v.getPermissionValue());
            }
        });
        return persmissons;
    }

    @Override
    public List<Permission> getAllPermission(String adminName) {
        return adminMapper.getAdminPermissions(adminName);
    }

    @Override
    public List<AdminRoleVo> getAdmin() {
        return adminMapper.getAdmin();
    }

}
